﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;
using System.Text.RegularExpressions;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     OrderStatusLog业务逻辑实现
    /// 作者：     Wilson
    /// 日期：     2011/12/4
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class OrderStatusLogBLL
    {
        private static OrderStatusLogBLL instance = new OrderStatusLogBLL();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IOrderStatusLogDao dao = null;

        private OrderStatusLogBLL()
        {
            dao = OrderStatusLogSqlMapDao.GetInstance();
        }

        public static OrderStatusLogBLL GetInstance()
        {
            return instance;
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<OrderStatusLog> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<OrderStatusLog> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<OrderStatusLog> GetListByParameters(object TopCounts, OrderStatusLog orderstatuslog)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<OrderStatusLog> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="LogId"></param>
        /// <returns></returns>
        public OrderStatusLog GetDetail(System.String logid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(logid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="orderstatuslog"></param>
        /// <returns></returns>
        public object Add(OrderStatusLog orderstatuslog)
        {
            //int id = GetId("SYS_UserInfoTemp");
            //sys_userinfotemp.UserID = id;

            try
            {
                ///daoManager.BeginTransaction();

                return dao.Add(orderstatuslog);
                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="orderstatuslog"></param>
        /// <returns></returns>
        public int Update(OrderStatusLog orderstatuslog)
        {
            try
            {
                ///daoManager.BeginTransaction();
                orderstatuslog.UpdateDate = DateTime.Now;

                return dao.Update(orderstatuslog);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="LogId"></param>
        /// <returns></returns>
        public int Delete(System.String logid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(logid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="orderstatuslog"></param>
        /// <returns></returns>
        public int Delete(OrderStatusLog orderstatuslog)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(orderstatuslog);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        #region##通过订单ID得到订单状态日志
        /// <summary>
        /// 通过订单ID得到订单状态日志
        /// 创建人：Wilson
        /// 创建时间：2011-12-4
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public OrderStatusLog GetOrderStatusLogByOrderId(string orderId)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.GetOrderStatusLogByOrderId(orderId);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        #endregion

        #region##得到新的LogContent字段
        /// <summary>
        /// 得到新的LogContent字段
        /// 创建人：Wilson
        /// 创建时间：2011-12-4
        /// </summary>
        /// <param name="oldLogContent"></param>
        /// <param name="statusId"></param>
        /// <returns></returns>
        public string GetNewLogContent(string oldLogContent, int statusId)
        {
            string result = string.Empty;  //返回结果

            Regex re = new Regex("(?<status>\\d+)\\:\\d+", RegexOptions.None);   //匹配所有（状态：时间戳）
            MatchCollection mc = re.Matches(oldLogContent);
            List<int> list = new List<int>();
            foreach (Match ma in mc)
            {
                list.Add(Convert.ToInt32(ma.Groups["status"].Value));
            }

            if (list.Count > 0)
            {
                //if (!list.Contains(statusId))  //如果没有记录过这个状态
                //{
                result = oldLogContent + "$" + statusId + ":" + AUV5.Common.OrderHelper.GetTimeStamp(DateTime.Now);
                // }
            }
            else
            {
                result = statusId + ":" + AUV5.Common.OrderHelper.GetTimeStamp(DateTime.Now);
            }
            return result;
        }
        #endregion

        #region##通过订单ID得到订单改变流程集合
        /// <summary>
        /// 通过订单ID得到订单改变流程集合
        /// 创建人：Wilson
        /// 创建时间：2011-12-6
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<AUV5.Model.ReadOrderStatusLog> GetListReadOrderStatus(string orderId)
        {
            try
            {
                AUV5.Model.OrderStatusLog statusLog = GetOrderStatusLogByOrderId(orderId);
                List<AUV5.Model.ReadOrderStatusLog> list = new List<ReadOrderStatusLog>();
                if (statusLog != null)
                {
                    Regex re = new Regex("(?<status>\\d+)\\:(?<timeStamp>\\d+)", RegexOptions.None);   //匹配所有（状态：时间戳）
                    MatchCollection mc = re.Matches(statusLog.LogContent);
                    foreach (Match ma in mc)
                    {
                        AUV5.Model.ReadOrderStatusLog readStatusLog = new ReadOrderStatusLog();
                        readStatusLog.StatusId = Convert.ToInt32(ma.Groups["status"].Value);
                        readStatusLog.CreateTime = AUV5.Common.OrderHelper.RestoreTimeStamp(ma.Groups["timeStamp"].Value);
                        list.Add(readStatusLog);
                    }
                }
                return list;
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        #endregion

        #region##最后状态是否是请求关闭
        /// <summary>
        /// 最后状态是否是请求关闭
        /// 创建人：Wilson
        /// 创建时间：2011-12-5
        /// </summary>
        /// <param name="logContent"></param>
        /// <returns></returns>
        public bool IsRequestClosed(string logContent)
        {
            bool result = false;
            Regex re = new Regex("(?<status>\\d+)\\:\\d+", RegexOptions.None);   //匹配所有（状态：时间戳）
            MatchCollection mc = re.Matches(logContent);
            List<int> list = new List<int>();
            foreach (Match ma in mc)
            {
                list.Add(Convert.ToInt32(ma.Groups["status"].Value));
            }

            if (list.Count > 0) 
            {
                int lastStatusId = list[list.Count - 1];
                if (lastStatusId == Convert.ToInt32(AUV5.Common.Enum.OrderState.SellerRequestClosed) || lastStatusId == Convert.ToInt32(AUV5.Common.Enum.OrderState.BuyerRequestClosed))
                {
                    result = true;
                }                
            }
            return result;
        }
        #endregion

    }
}
